package com.github.camera;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ImageUtils;
import com.github.camera.utils.CameraUtil;
import com.yalantis.ucrop.UCrop;

import java.io.File;

public class BasePictureActivity extends AppCompatActivity {

    public void setCrop(Uri uri, Activity context) {
        UCrop.Options options = new UCrop.Options();
        int toolbarColor = getResources().getColor(R.color.black);
        int statusColor = getResources().getColor(R.color.black);
        int titleColor = getResources().getColor(R.color.white);
        options.setToolbarColor(toolbarColor);
        options.setStatusBarColor(statusColor);
        options.setToolbarWidgetColor(titleColor);
        options.setCircleDimmedLayer(false);
        options.setShowCropFrame(true);
        options.setShowCropGrid(false);
        options.setCompressionQuality(90);
        options.setHideBottomControls(true);
        options.setFreeStyleCropEnabled(true);

        String realFilePath = CameraUtil.getRealFilePath(this, uri);
        String imgType = CameraUtil.getLastImgType(realFilePath);

        UCrop.of(uri, Uri.fromFile(new File(CameraUtil.getDiskCacheDir(context), System.currentTimeMillis() + imgType))).withAspectRatio(0, 0).withMaxResultSize(100, 100).withOptions(options).start(context);
    }

    public String getGalleryPhoto(Uri uri) {
        return CameraUtil.getRealFilePath(this, uri);
    }

    public Uri saveImageToGallery(Context context, Bitmap bmp) {
        File file = ImageUtils.save2Album(bmp, Bitmap.CompressFormat.JPEG);
        // 首先保存图片
        final Uri uri = Uri.parse("file://" + file);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
        return uri;
    }
}
